package com.yjf.ljr.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonArray;
import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.BorrowingDetailDataClass;
import com.yjf.ljr.dataclass.ProjectInfoBorrowDataClass;
import com.yjf.ljr.dataclass.BorrowingDetailDataClass.DemandInfoData;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.PicLoad;
import com.yjf.ljr.util.SPreferences;
import com.yjf.ljr.util.ImageLoader.ImageCallback;

public class BorrowingDetailsActivity extends IjActivity {

	@ID("tvGender")
	private TextView mTvGender;// 性别
	@ID("tvAge")
	private TextView mTvAge;// 年龄
	@ID("tvEducation")
	private TextView mTvEducation;// 文化程度
	@ID("tvMarried")
	private TextView mTvMarried;// 是否结婚
	@ID("tvHouseholdRegister")
	private TextView mTvHouseholdRegister;// 户籍
	@ID("tvSalaryLevel")
	private TextView mTvSalaryLevel;// 薪资水平
	@ID("tvOtherLoans")
	private TextView mTvOtherLoans;// 其他贷款
	@ID("tvOtherAssets")
	private TextView mTvOtherAssets;// 其他资产
	@ID("tvDuty")
	private TextView tvDuty;// 职务
	@ID("tvCompanyType")
	private TextView tvCompanyType;// 公司性质
	@ID("ll_borrowingdetaile")
	private LinearLayout ll_borrowingdetaile;
	@ID("projectInfo")
	private WebView projectInfo;

	private String demandId;// 项目id
	@ID("fail_borrowingdetaile")
	// 加载失败显示界面
	private LinearLayout mLlfail_borrowingdetaile;

	private String loanNote;
	@ID("lljg")
	private LinearLayout mLlllg;// 企业
	@ID("llgr")
	private LinearLayout mLlllgr;// 个人
	@ID("tvinfo4")
	private WebView mTvtvinfo4;//
	@ID("tvinfo5")
	private WebView mTvtvinfo5;//
	@ID("tvinfo6")
	private WebView mTvtvinfo6;//

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_borrow_detail);
		initControl();
	}

	private void initControl() {

		setLeftBtnClick();
		setTitleStr("借款详情");
		mLlfail_borrowingdetaile.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showProgressDialog();
				new BorringActivityTask(demandId).execute();
			}
		});
		demandId = getIntent().getStringExtra("demandId");

		WebSettings settings = projectInfo.getSettings();
		projectInfo.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settings.setSupportZoom(false);
		settings.setBuiltInZoomControls(false);
		settings.setDomStorageEnabled(true);
		settings.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDir = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settings.setAppCachePath(appCacheDir);
		settings.setAllowFileAccess(true);
		settings.setAppCacheEnabled(true);
		settings.setCacheMode(WebSettings.LOAD_DEFAULT);
		/******************/
		WebSettings settingsinfo4 = mTvtvinfo4.getSettings();
		mTvtvinfo4.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settingsinfo4.setSupportZoom(false);
		settingsinfo4.setBuiltInZoomControls(false);
		settingsinfo4.setDomStorageEnabled(true);
		settingsinfo4.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDirinfo4 = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settings.setAppCachePath(appCacheDirinfo4);
		settings.setAllowFileAccess(true);
		settings.setAppCacheEnabled(true);
		settings.setCacheMode(WebSettings.LOAD_DEFAULT);

		/******************/
		WebSettings settingsinfo5 = mTvtvinfo5.getSettings();

		mTvtvinfo5.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settingsinfo5.setSupportZoom(false);
		settingsinfo5.setBuiltInZoomControls(false);
		settingsinfo5.setDomStorageEnabled(true);
		settingsinfo5.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDirinfo5 = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settingsinfo5.setAppCachePath(appCacheDirinfo5);
		settingsinfo5.setAllowFileAccess(true);
		settingsinfo5.setAppCacheEnabled(true);
		settingsinfo5.setCacheMode(WebSettings.LOAD_DEFAULT);
		/******************/
		WebSettings settingsinfo6 = mTvtvinfo6.getSettings();
		mTvtvinfo6.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settingsinfo6.setSupportZoom(false);
		settingsinfo6.setBuiltInZoomControls(false);
		settingsinfo6.setDomStorageEnabled(true);
		settingsinfo6.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDirinfo6 = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settingsinfo6.setAppCachePath(appCacheDirinfo6);
		settingsinfo6.setAllowFileAccess(true);
		settingsinfo6.setAppCacheEnabled(true);
		settingsinfo6.setCacheMode(WebSettings.LOAD_DEFAULT);

		loanNote = getIntent().getStringExtra("loanNote");

		showProgressDialog();
		new BorringActivityTask(demandId).execute();
	}

	class BorringActivityTask extends AsyncTask<Void, Void, Void> {
		private String projectId;// 项目ID
		private String mFailLog;
		private BorrowingDetailDataClass date = new BorrowingDetailDataClass();

		public BorringActivityTask(String projectId) {
			this.projectId = projectId;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showProgressDialog();
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			RequestObject requestObjectSms = new RequestObject();
			requestObjectSms.method = "loanerInfo";
			requestObjectSms.map.put("demandId", demandId);

			String requestSmsUrl = RequestBuilder.build(requestObjectSms);
			try {
				IjConnect.doRequest(requestSmsUrl, date);
				if (date.code == null) {
					mFailLog = getResources().getString(R.string.loaddata_exception);
				} else if (!date.code.equals(CommonData.RESULT_SUCCESS)) {
					mFailLog = date.msg;
				}
			} catch (Exception e) {
				mFailLog = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			dismissProgressDialog();
			if ("-1".equals(date.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(mFailLog)) {
				showToast(mFailLog);
				mLlfail_borrowingdetaile.setVisibility(View.VISIBLE);
				ll_borrowingdetaile.setVisibility(View.GONE);
			} else {
				mLlfail_borrowingdetaile.setVisibility(View.GONE);
				ll_borrowingdetaile.setVisibility(View.VISIBLE);
				DemandInfoData data = (DemandInfoData) date.list;
				if (date.code != null && date.code.equals(CommonData.RESULT_SUCCESS)) {
					if (!TextUtils.isEmpty(date.userType) && (date.userType.equals("GR"))) {
						mLlllg.setVisibility(View.GONE);// 企业
						mLlllgr.setVisibility(View.VISIBLE);// 个人
						if (!TextUtils.isEmpty(data.gender)) {
							mTvGender.setText(data.gender);// 性别
						}
						if (!TextUtils.isEmpty(data.age)) {
							mTvAge.setText(data.age);// 年龄
						}
						if (!TextUtils.isEmpty(data.education)) {
							mTvEducation.setText(data.education);// 文化程度
						}
						if (!TextUtils.isEmpty(data.married)) {
							mTvMarried.setText(data.married);// 是否结婚
						}
						if (!TextUtils.isEmpty(data.householdRegister)) {
							mTvHouseholdRegister.setText(data.householdRegister);// 户籍
						}
						if (!TextUtils.isEmpty(data.salaryLevel)) {
							mTvSalaryLevel.setText(data.salaryLevel);// 薪资水平
						}
						if (!TextUtils.isEmpty(data.otherLoans)) {
							mTvOtherLoans.setText(data.otherLoans.trim());// 其他贷款
						}
						if (!TextUtils.isEmpty(data.otherAssets)) {
							mTvOtherAssets.setText(data.otherAssets.trim());// 其他资产
						}

						if (!TextUtils.isEmpty(data.info1)) {
							tvDuty.setText(data.info1.trim());
						}
						if (!TextUtils.isEmpty(data.info2)) {
							tvCompanyType.setText(data.info2.trim());
						}

						if (!TextUtils.isEmpty(loanNote)) {
							projectInfo.loadDataWithBaseURL(null, loanNote, "text/html", "utf-8", null);
						}
					} else if (!TextUtils.isEmpty(date.userType) && (date.userType.equals("JG"))) {
						mLlllg.setVisibility(View.VISIBLE);// 企业
						mLlllgr.setVisibility(View.GONE);// 个人
						mTvtvinfo4.loadDataWithBaseURL(null, data.info4, "text/html", "utf-8", null);
						mTvtvinfo5.loadDataWithBaseURL(null, data.info5, "text/html", "utf-8", null);
						mTvtvinfo6.loadDataWithBaseURL(null, data.info6, "text/html", "utf-8", null);

					}

				}
			}
		}
	}

}
